<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="zh-cn" style="height: 100%">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
    <title><?=VIEW_TITLE?></title>
    <link href="<?php echo base_url('/assets/bs/'); ?>css/bootstrap.min.css" rel="stylesheet">
    <link href="<?php echo base_url('/assets/css/'); ?>my_common.css" rel="stylesheet">
    <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
</head>
<body style="min-width: 720px;">
<?php
$this->load->view('admin_header.php');
?>
<div class="container">
    <div class="row">
        <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>
        <div id="main_div" class="col-xs-10 col-sm-10 col-md-10 col-lg-10" style="min-width: 720px;margin: auto;">
            <div id="find_div" style="position: relative;">
                <div class="input-group" style="width: 50%;margin: auto;">
                    <input type="text" class="form-control" id="name" name="" placeholder="">
                    <span class="input-group-btn">
                        <button id="btn_searh" class="btn btn-primary" type="button" onclick="search()">搜索!</button>
                    </span>

                </div>
                <div style="position: absolute;top: 0;right: 0px;">
                    <button id="cpassword_btn" class="btn btn-primary" type="button" onclick="openCpModal()">修改密码
                    </button>
                </div>
                <div style="text-align: center;">
                    <span style="font-size: 10px;color: #adadad;">留空搜索默认显示全部用户!</span>
                </div>
            </div>

            <table class="table table-striped" style="margin-bottom: 0;">

            </table>
            <div id="scroll_div" style="overflow-y: auto;overflow-x: hidden;">
                <table class="table table-striped" id="table">

                </table>
                <div>
                    <nav style="text-align: center;">
                        <ul class="pagination" id="page_ul">
                        </ul>
                    </nav>
                </div>

            </div>
        </div>
    </div>
    <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>
</div>
<div class="modal fade" id="cpassword_model">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                        class="sr-only">Close</span></button>
                <h4 class="modal-title">修改密码</h4>
            </div>
            <div class="modal-body">
                <div>
                    <label for="username" class="col-sm-2 control-label">账号</label>
                    <div>
                        <input type="text" class="form-control" id="cp_username" placeholder="需要修改密码的账号">
                    </div>
                    <label for="password1" class="col-sm-2 control-label">密码</label>
                    <div>
                        <input type="text" class="form-control" id="cp_password1" placeholder="密码">
                    </div>
                    <label for="password2" class="col-sm-2 control-label">确认密码</label>
                    <div>
                        <input type="text" class="form-control" id="cp_password2" placeholder="确认密码">
                    </div>
                </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="cpassword()">确认修改!</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<?php
$this->load->view('footer.php');
?>
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<script src="<?php echo base_url('/assets/bs/'); ?>js/bootstrap.min.js"></script>
<script>


    var w_width = document.documentElement.clientWidth;
    var w_height = document.documentElement.clientHeight;
    var scroll_div_height = w_height - 250;
    $('#scroll_div').height(scroll_div_height + 'px');
    var this_page_sum = 0;
    var this_page = 1;
    var this_rows = 15;
    var this_name = '';
    var pagination_show_num = 7;
    function buildPageUl(count) {
        var page_num = Math.ceil(count / this_rows);
        this_page_sum = page_num;
        var html = '';
        html += '<li><a href="javascript:getPage(1)">&laquo;</a></li><li class=""><a href="javascript:lastPage()">上一页</a></li>';
        for (var i = 0; i < page_num; i++) {
            if (this_page == (i + 1)) {
                html += '<li class="active"><a href="javascript:getPage(' + (i + 1) + ')">' + (i + 1) + '</a></li>';
            } else {
                html += '<li class=""><a href="javascript:getPage(' + (i + 1) + ')">' + (i + 1) + '</a></li>';
            }
        }
        html += ' <li class=""><a href="javascript:nextPage()">下一页</a></li><li><a href="javascript:getPage(' + page_num + ')">&raquo;</a></li>';
        $('#page_ul').empty();
        $('#page_ul').append(html);
        pagination_col(page_num);
    }

    function pagination_col(page_num) {
        var begin = 0;
        var finish = 0;
        if (page_num > pagination_show_num) {//需要显示更多了
//            console.log(this_page % pagination_show_num);
            if (this_page != 0 && this_page % pagination_show_num == 0) {//要切换分页组了
                begin = this_page;
                finish = (this_page * 1 + pagination_show_num) * 1;

            } else {
                finish = Math.ceil(this_page / pagination_show_num) * pagination_show_num;
                begin = finish - 7;
            }
            if (finish > page_num) {
                finish = page_num;
            }
            //判断是否还有下一组
            if ((finish - begin) < 7) {
                $("#shenglue_page").hide();
            }
            for (var i = begin; i <= finish; i++) {
                $('#pagination_' + i).show();
            }
        } else {
            for (var i = 1; i <= 7; i++) {
                $('#pagination_' + i).show();
            }
        }
    }
    function search() {
        this_name = $('#name').val();
        getUserListJson(this_name);
    }

    function stopUser(user_id) {
        $.ajax({
            url: '<?php echo site_url('/Administrator/stopUser')?>',
            type: 'post',
            dataType: 'json',
            data: {
                user_id: user_id
            },
            success: function (data) {
                if (data == 'success') {
                    $('#btn_' + user_id).attr('onclick', 'startUser(' + user_id + ')');
                    $('#btn_' + user_id).attr('class', 'btn btn-success btn-xs');
                    $('#btn_' + user_id).text('启用');
                }
            }
        });
    }
    function startUser(user_id) {
        $.ajax({
            url: '<?php echo site_url('/Administrator/startUser')?>',
            type: 'post',
            dataType: 'json',
            data: {
                user_id: user_id
            },
            success: function (data) {
                if (data == 'success') {
                    $('#btn_' + user_id).attr('onclick', 'stopUser(' + user_id + ')');
                    $('#btn_' + user_id).attr('class', 'btn btn-danger btn-xs');
                    $('#btn_' + user_id).text('停用');
                }
            }
        });
    }

    function getPage(page_num) {
        this_page = page_num;
        getUserListJson(this_name);
    }
    function nextPage() {
        if (this_page < this_page_sum) {
            ++this_page;
        }
        getUserListJson(this_name);
    }
    function lastPage() {
        if (this_page != 1) {
            --this_page;
        }
        getUserListJson(this_name);
    }

    function getUserListJson(name) {
        $.ajax({
            url: '<?php echo site_url('/Administrator/getUserListJson')?>',
            type: 'post',
            dataType: 'json',
            data: {
                page: this_page,
                rows: this_rows,
                name: name
            },
            success: function (data) {
                console.log(data);
                var length = data['list'].length;
//                var html = '<tr style="display: none;"><td></td></tr>';
                var html = '<tr><th>用户名</th><th>账号</th><th>等级</th><th>学校专业</th><th>所在地</th><th>启动/停用</th></tr>';
                for (var i = 0; i < length; i++) {
                    html += '<tr id="user_div_' + data['list'][i]['id'] + '">';
                    html += '<td>' + data['list'][i]['name'] + '</td>';
                    html += '<td>' + data['list'][i]['username'] + '</td>';
                    html += '<td>' + data['list'][i]['level'] + '</td>';
                    html += '<td>' + data['list'][i]['school'] + '[' + data['list'][i]['professional'] + ']</td>';
                    html += '<td>' + data['list'][i]['address_life'] + '</td>';
                    html += '<td>';
                    if (data['list'][i]['is_stop'] == 0) {
                        html += '<button id="btn_' + data['list'][i]['id'] + '" style="width:60px" type="button" class="btn btn-danger btn-xs" onclick="stopUser(' + data['list'][i]['id'] + ')">停用</button>';
                    } else {
                        html += '<button id="btn_' + data['list'][i]['id'] + '" style="width:60px" type="button" class="btn btn-success btn-xs" onclick="startUser(' + data['list'][i]['id'] + ')">启用</button>';
                    }
                    if (data['list'][i]['type'] == 1) {
                        html += '<button style="margin-left: 5px;width:60px" id="up_btn_' + data['list'][i]['id'] + '" type="button" class="btn btn-warning btn-xs" onclick="upUser(' + data['list'][i]['id'] + ')">升级</button>';
                    } else {
                        html += '<span class="text-primary" style="display:inline-block;font-size: 8px;width: 60px;text-align: center;margin-left: 5px">任务专员</span>';
                    }
                    html += '</td></tr>';
                }
                $('#table').empty();
                $('#table').append(html);
                buildPageUl(data['count']);
            }
        });
    }

    function upUser(user_id) {
        $.ajax({
            url: '<?php echo site_url('/Administrator/upUser')?>',
            type: 'post',
            dataType: 'json',
            data: {
                user_id: user_id
            },
            success: function (data) {
                console.log(data);
                if (data == 'success') {
                    alert('升级成功');
                    $("#user_div_" + user_id).hide();
                } else {
                    alert(data);
                }
            }
        });
    }

    getUserListJson();
    function openCpModal() {
        $("#cpassword_model").modal("show");
    }

    function cpassword() {
        var password1 = $('#cp_password1').val();
        var password2 = $('#cp_password2').val();
        var username = $('#cp_username').val();
        if (password1 == '' || password2 == '' || username == '') {
            alert('账号或密码不能为空!');
            return false;
        }
        if (password1 != password2) {
            alert('两次输入密码不一致,请重新输入!');
            return false;
        }
        $.ajax({
            url: '<?php echo site_url('/Administrator/cpasswordChange')?>',
            type: 'post',
            dataType: 'json',
            data: {
                password1: password1,
                password2: password2,
                username: username
            },
            success: function (data) {
                alert(data);
                if(data == '密码修改成功!'){
                    $("#cpassword_model").modal("hide");
                    $('#cp_password1').val('');
                    $('#cp_password2').val('');
                    $('#cp_username').val('');
                }
            }
        });
    }
</script>
</body>
</html>